<template>
  <div>
    <!-- 地址 -->
    <div class="address" @click="$router.push('/address')">
    <!-- 有地址时展示 -->
      <div v-if="!address.username">
        <van-button type="primary">新增收货地址</van-button>
      </div>
      <!-- 没有地址时展示 -->
      <div v-else>
        <div>{{ address.username }} {{ address.userphone }}</div>
        <div class="bg" v-if="address.status == 1">默认</div>
        <div class="bg">{{ address.label }}</div>
        <div>{{ address.location + address.useraddress }}</div>
      </div>
    </div>
    <!-- 展示购物车的数据 -->
    <van-card
      v-for="item in shopList"
      :key="item.id"
      :title="item.goodsname"
      :thumb="$pre + item.img"
    >
      <div slot="price">￥{{ item.price | filterPrice }}</div>
      <div slot="num">
        <span class="num-span">{{ item.num }}</span>
      </div>
    </van-card>

    <!-- 总价 -->
    <div>总价:{{ allPrice | filterPrice }}</div>
    <van-button type="primary" @click="confirm">确认下单</van-button>
  </div>
</template>

<script>
import {reqShop,reqAddressList,reqOrder} from "../../http/http"
export default {
  data() {
    return {
      shopList: [],
      allPrice: 0,
      address: {},
    };
  },
  mounted(){
    
  },
  activated(){
      // 请求购物车数据
      // 先清空上一次的数据
      this.shopList = [];      
      // console.log(this.shopList);
      let idArr = JSON.parse( localStorage.getItem("shopInfo") ).idstr.split(",");//逗号分割 id
      // console.log(idArr);
      // 循环一遍取出数据
      idArr.forEach((id)=>{
        reqShop({
          uid:JSON.parse( localStorage.getItem("userInfo")).uid,
          id:id
        }).then(res=>{
        if(res.data.code === 200 ){
          // console.log(res);
          this.shopList.push( res.data.list[0]);
        }
      });
    });
    // 取出总价格
    this.allPrice = (JSON.parse( localStorage.getItem("shopInfo")).countmoney)/100;

    if(this.$route.query.addressId){
      reqAddressList({
        uid:JSON.parse(localStorage.getItem("userInfo")).uid,
        id:this.$route.query.addressId
      }).then(res=>{
        // console.log(res);
        if(res.data.code === 200){
          this.address = res.data.list[0]
        }
      })
    }else{
      // 找默认地址
      reqAddressList({
          uid:JSON.parse(localStorage.getItem("userInfo")).uid,
          id:this.$route.query.addressId
      }).then(res=>{
        // console.log(res);
        if(res.data.code==200){
            //如果有默认地址，返回的是数组，数组里面的数据  status =1
            // 如果没有默认地址，返回也是数组，数组里面没有一条status=1
            let arr=res.data.list?res.data.list:[]
            if(arr.every(item=>item.status!=1)){
              //没有默认地址 
              this.address={}
            }else{
              this.address=arr[0]
            }
        }
      })
    }
  },
  mounted() {},
  methods:{
    confirm(){

    }
  }
};
</script>

<style>
</style>